package com.example.demopoi.importtxt;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TXTParseUtils {

    private static final Integer ONE = 1;

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:/value.txt")),
                    "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {//数据以逗号分隔
                System.out.println(lineTxt);
                String[] names = lineTxt.split(",");
                for (String name : names) {
                    if (map.keySet().contains(name)) {
                        map.put(name, (map.get(name) + ONE));
                    } else {
                        map.put(name, ONE);
                    }
                }
            }
            br.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }

        /* 输出数据 */
//        try {
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/value_map.txt")),
//                    "UTF-8"));
//
//            for (String name : map.keySet()) {
//                bw.write(name + " " + map.get(name));
//                bw.newLine();
//            }
//            bw.close();
//        } catch (Exception e) {
//            System.err.println("write errors :" + e);
//        }
        findOrganAccountToExcel();
    }

    public static void findOrganAccountToExcel() {
        OutputStream out = null;
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook book = new HSSFWorkbook();
        //创建sheet对象（excel的表单）
        HSSFSheet sheet = book.createSheet("sheet1");

        //创建第一行，这里即是表头。行的最小值是0，代表每一行，上限没研究过，可参考官方的文档
        HSSFRow row1 = sheet.createRow(0);
        //在这一行创建单元格，并且将这个单元格的内容设为“账号”，下面同理。
        //列的最小值标识也是0
        row1.createCell(0).setCellValue("账号");
        row1.createCell(1).setCellValue("用户名");
        row1.createCell(2).setCellValue("日期");
        row1.createCell(3).setCellValue("是否完成");

        //第二行
        HSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("123456");
        row2.createCell(1).setCellValue("张三");
        row2.createCell(2).setCellValue("2019-08-05");
        row2.createCell(3).setCellValue("是");

        //第三行
        HSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("5681464");
        row3.createCell(1).setCellValue("李四");
        row3.createCell(2).setCellValue("2019-08-01");
        row3.createCell(3).setCellValue("否");

        File file = new File("e:\\");
        OutputStream stream = null;
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
            stream = new FileOutputStream(new File(file, "机构客户信息表_" + sdf.format(date) + ".xls"));
            //document.write(stream);
            book.write(stream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) ;
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
